﻿using SqlSugar;
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Zhao.Common.Helper;
using Zhao.IServer;
using Zhao.Models.DTO;
using Zhao.Models.Entity;
using Zhao.Repository.Base;
using Zhao.Repository.UnitOfWork;

namespace Zhao.Service
{
    public class BillServer : BaseService<Bill>, IBillServer
    {
        public BillServer(IBaseRepository<Bill> baseDal, IUnitOfWork unitOfWork) : base(baseDal, unitOfWork)
        {
        }

        
        /// <summary>
        /// 查询商户的订单
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public async Task<PageModel<BillDto>> QueryPageBill(Expression<Func<Bill,Venues,bool>> expression,int pageIndex,int pageSize)
        {
            //获取总条数
            RefAsync<int> totalCount = 0;

            //根据球馆不同展示不同的数据
            var list=await _Client.Queryable<Bill>()
                .LeftJoin<Venues>((a,b)=>a.VenuesId==b.VenueID)
                .WhereIF(expression!=null,expression)
                .Select((a, b) => new BillDto
                {
                    Id=a.Id,
                    Amount=a.Amount,
                    VName=b.VenueName,
                    TId=a.Type,
                    Status=a.Status,
                    OrderNo=a.RelatedOrderNo,
                    TransactionNo=a.TransactionNo,
                    Remark=a.Remark,
                    AId=a.ActivityId,
                    AppId=a.AppointmentsId
                }).ToPageListAsync(pageIndex, pageSize, totalCount).ConfigureAwait(false);

            int totalPage = (Math.Ceiling(totalCount.ObjToDecimal() / pageSize.ObjToDecimal())).ObjToInt();

            return new PageModel<BillDto>() { count = totalCount, PageCount = totalPage, PageIndex = pageIndex, PageSize = pageSize, data = list, code = totalCount == 0 ? -1 : 0, msg = totalCount == 0 ? "未查询到任何数据" : "" };
        }


        /// <summary>
        /// 审核提现
        /// </summary>
        /// <param name="billReview"></param>
        /// <returns></returns>
        public async Task<bool> Review(BillReviewDto billReview)
        {
            var rows=await _Client.Updateable<Bill>()
                .SetColumns(b=>new Bill
                {
                    Status= billReview.Status,
                    Remark= billReview.Msg,
                    RelatedOrderNo=billReview.OrderNumber
                })
                .Where(b=>b.Id== billReview.Id)
                .ExecuteCommandAsync();

            return rows > 0;
        }
    }
}
